package com.example.demo.Server.impl;

import com.example.demo.Server.Look_for_jobServer;
import com.example.demo.entity.DeliveryRecord;
import com.example.demo.entity.JobPosition;
import com.example.demo.mapper.Look_for_jpb_Mapper;
import com.example.demo.result.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class Look_for_job_impl implements Look_for_jobServer {

    @Autowired
    private Look_for_jpb_Mapper look_for_jpb_mapper;
    @Override
    public ArrayList<JobPosition> select_Look_for_jon() {

        return look_for_jpb_mapper.select_Look_for_jon();
    }

    @Override
    public int add_job(JobPosition jobPosition) {
        return look_for_jpb_mapper.add_job(jobPosition);
    }

    @Override
    public int delete_job(Integer id) {
        return look_for_jpb_mapper.delete_job(id);
    }

    @Override
    public int update_job(JobPosition jobPosition) {
        return look_for_jpb_mapper.update_job(jobPosition);
    }

    @Override
    public PageResult select_Look_for_jon_page(int page, int size,int  hrId,JobPosition jobPosition) {

        PageHelper.startPage(page,size);
        Page<JobPosition> jobPositions =  look_for_jpb_mapper.select_Look_for_jon_page(page, size,hrId,jobPosition);
        List<JobPosition> result = jobPositions.getResult();
        long total = jobPositions.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public boolean deliverResume(Integer userId, Integer jobId, Integer hrId, String resumePath) {
        // 检查是否已投递过该岗位
        int count = look_for_jpb_mapper.countByUserIdAndJobId(userId, jobId);
        if (count > 0) {
            // 已投递过，返回false
            return false;
        }

        // 创建投递记录
        DeliveryRecord record = new DeliveryRecord(userId, jobId, hrId, resumePath);

        // 插入数据库
        int result = look_for_jpb_mapper.insertDeliveryRecord(record);

        return result > 0;
    }

    @Override
    public List<Integer> getDeliveredJobIds(Integer userId) {
        return look_for_jpb_mapper.selectDeliveredJobIdsByUserId(userId);
    }

    @Override
    public List<Integer> getDeliveryJobIdsByUserId(Integer userId) {
        return look_for_jpb_mapper.getDeliveryJobIdsByUserId(userId);
    }

    @Override
    public List<JobPosition> getDeliveredJobsByUser(Integer userId) {
        // 先获取用户投递的所有岗位ID
        List<Integer> jobIds = look_for_jpb_mapper.getDeliveryJobIdsByUserId(userId);

        // 如果没有投递记录，返回空列表
        if (jobIds == null || jobIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 根据岗位ID列表获取岗位详情
        return look_for_jpb_mapper.getJobsByJobIds(jobIds);
    }

    @Override
    public List<DeliveryRecord> getDeliveryRecordsWithDetailsByUserId(Integer userId) {
        return look_for_jpb_mapper.getDeliveryRecordsWithDetailsByUserId(userId);
    }

    @Override
    public List<DeliveryRecord> getDeliveryRecordsByHrId(Integer hrId) {
        return look_for_jpb_mapper.getDeliveryRecordsByHrId(hrId);
    }

    @Override
    public List<Map<String, Object>> getDeliveryRecordsWithDetailsByHrId(Integer hrId) {
        return look_for_jpb_mapper.getDeliveryRecordsWithDetailsByHrId(hrId);
    }


}



